package com.atwulidun.stack;

import java.util.Scanner;

public class ArrayStackDemo {
    public static void main(String[] args) {
        ArrayStack arrayStack = new ArrayStack(4);
        Scanner scanner = new Scanner(System.in);
        String key = "";
        boolean loop = true;
        while (loop) {
            System.out.println("show：显示栈");
            System.out.println("exit：退出栈");
            System.out.println("push：进栈");
            System.out.println("pop：出栈");
            System.out.println("请输入：");
            key = scanner.next();
            switch (key) {
                case "show":
                    try {
                        arrayStack.list();
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case "exit":
                    scanner.close();
                    loop = false;
                    System.out.print("程序退出！");
                    break;
                case "push":
                    System.out.println("请输入入栈整数：");
                    int value = scanner.nextInt();
                    arrayStack.push(value);
                    break;
                case "pop":
                    try {
                        System.out.println("出栈数据为：" + arrayStack.pop());
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
            }
        }
    }
}

class ArrayStack {
    private int maxSize;
    private int top = -1;
    private int[] stack;

    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[maxSize];
    }

    public boolean isFull() {
        return top == (maxSize - 1);
    }

    public boolean isEmpty() {
        return top == -1;
    }

    public void push(int value) {
        if (isFull()) {
            System.out.println("栈满，不能再压栈！");
            return;// 因为该方法没有返回值，因此直接return即可
        }
        top++;
        stack[top] = value;
    }

    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈空，不能出栈！");// 因为该方法有返回值，返回null没有意义，因此抛出一个异常
        }
        int value = stack[top];
        top--;
        return value;
    }

    public void list() {
        if (isEmpty()) {
            System.out.println("栈为空！");
            return;
        }
        for (int i = top; i >= 0; i--) {
            System.out.printf("stack[%d] = %d\n", i, stack[i]);
        }
    }
}

//class ArrayStack {
//    private int[] stack;
//    private int maxSize;
//    private int top;
//
//    public ArrayStack(int maxSize) {
//        this.maxSize = maxSize;
//        stack = new int[maxSize];
//        top = -1;
//    }
//
//    public boolean isEmpty() {
//        return top == -1;
//    }
//
//    public boolean isFull() {
//        return top == maxSize - 1;
//    }
//
//    public void push(int num) {
//        if (isFull()) {
//            System.out.println("the stack is full!");
//            return;
//        }
//        stack[++top] = num;
//    }
//
//    public int pop() {
//        if (isEmpty()) {
//            throw new RuntimeException("the stack is empty!");
//        }
//        int temp = stack[top];
//        stack[top] = 0;
//        top--;
//        return temp;
//    }
//
//    public void list() {
//        if (isEmpty()) {
//            throw new RuntimeException("the stack is empty!");
//        }
//        for (int i = top; i >= 0; i--) {
//            System.out.printf("arr[%d]=%d\n", i, stack[i]);
//        }
//    }
//}